import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    managementList: []
  },

  toDetail(event) {
    const id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/management/add-management/index?id=" + id
    });
  },

  // 删除
  onDelete(event) {
    const id = event.target.dataset.id;
    Dialog.confirm({
      message: "确定删除吗？"
    }).then(() => {
      this.delete(id);
    });
  },

  // 删除亦是解绑
  delete(id) {
    // debugger
    wx.showLoading({
      title: "删除中..."
    });
    wx.$apis
      .deleteMarketUser({
        marketUserIds: [id],
        marketRoleId: 1
      })
      .then((res) => {
        this.getData();
        if (!res.data) {
          return wx.showToast({
            title: "删除失败，请稍后再试~",
            icon: "none",
            duration: 2000
          });
        }
        wx.showToast({
          title: "删除成功",
          icon: "success",
          duration: 2000
        });
      });
  },

  getData() {
    wx.showLoading({
      title: "获取中..."
    });
    wx.$apis
      .getMarketUser({
        realName: this.data.searchValue,
        current: 1,
        size: 1000,
        companyId: app.globalData.userInfo.companyData.companyId,
        marketRoleId: 1
      })
      .then((res) => {
        if (res.data) {
          this.setData({
            managementList: res.data.records
          });
        }
      });
  },

  toAddManagement() {
    wx.navigateTo({
      url: "/pages/management/add-management/index"
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
